class Sqlc < Formula
  desc "Generate type safe Go from SQL"
  homepage "https://sqlc.dev/"
  url "https://github.com/sqlc-dev/sqlc/archive/refs/tags/v1.25.0.tar.gz"
  sha256 "749f8b5cc4aceb5b15676024ffb2e5cb661e3bc40cdf6c5cf0043bd8c398d8c2"
  license "MIT"
  head "https://github.com/sqlc-dev/sqlc.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_sonoma:   "f195848f1d2fe889d78b6af576964e534b21f0158b03afe009624d4b0ab64ac2"
    sha256 cellar: :any_skip_relocation, arm64_ventura:  "b908a0dc3040b731cb50d6cf3ca0379926822fb7a87255c7ea1efb3f0948d9de"
    sha256 cellar: :any_skip_relocation, arm64_monterey: "04d37f9bf5b6d807902a97b15149286775a5571809f618e9fe4e650a82d22f33"
    sha256 cellar: :any_skip_relocation, sonoma:         "ca8cbbfc0ac27c5885c4f682ca7712d77e3bbbec518669e6d2cc5ba10cd37d09"
    sha256 cellar: :any_skip_relocation, ventura:        "722f77e6b2b1e266f8a5ad46250a819a87d55cfde76042153b8dffcca4ec1c1a"
    sha256 cellar: :any_skip_relocation, monterey:       "af06ebcfcf2234ed4d97673153b5ca7b260070abe4679e8a9ec1a5812968af19"
    sha256 cellar: :any_skip_relocation, x86_64_linux:   "b50765394f6e8129a98a3723bcbb5759a9c84217f358158950f80e7966f1b7e8"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w"), "./cmd/sqlc"

    generate_completions_from_executable(bin/"sqlc", "completion")
  end

  test do
    (testpath/"sqlc.json").write <<~SQLC
      {
        "version": "1",
        "packages": [
          {
            "name": "db",
            "path": ".",
            "queries": "query.sql",
            "schema": "query.sql",
            "engine": "postgresql"
          }
        ]
      }
    SQLC

    (testpath/"query.sql").write <<~EOS
      CREATE TABLE foo (bar text);

      -- name: SelectFoo :many
      SELECT * FROM foo;
    EOS

    system bin/"sqlc", "generate"
    assert_predicate testpath/"db.go", :exist?
    assert_predicate testpath/"models.go", :exist?
    assert_match "// Code generated by sqlc. DO NOT EDIT.", File.read(testpath/"query.sql.go")
  end
end
